<?
	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);

	$MDOS_Srv_List= array(	"10.100.160.238", "172.17.0.56",
							"10.100.160.246", "172.17.0.63",
							"10.100.160.242", "172.17.0.60",
							"10.100.160.240", "172.17.0.58",
							"10.100.160.239", "172.17.0.57"
						);

	if (isset($ip) && isset($kid)){
		$ip=trim($ip);
		$kid=trim($kid);
		$isFound="Fail";

		if(in_array($ip, $MDOS_Srv_List)){
			$servername="DRIVER={SQL Server};SERVER=10.100.160.176;DATABASE=ReportServer";
			$conn=odbc_connect($servername,'thtfmes',"Mes&thtf",SQL_CUR_USE_ODBC);
			if ($conn){
				$sql1="SELECT Name FROM Master..SysDatabases";
				$exec1=odbc_exec($conn, $sql1);
				while(odbc_fetch_row($exec1)){
					$sql="	SELECT ProductKeyID, ProductKeyStateID, ProductKeyState, HardwareID
								FROM ".odbc_result($exec1,"Name").".dbo.ProductKeyInfo 
							WHERE (ProductKeyID = '$kid')";

					$exec=odbc_exec($conn, $sql);
					while(odbc_fetch_row($exec))
					{
						echo ":Product Key State:".odbc_result($exec,"ProductKeyState")."\r\n";
						echo "set KeyStateID=".odbc_result($exec,"ProductKeyStateID")."\r\n";
						if(odbc_result($exec,"ProductKeyStateID")>=3){
							echo "oa3tool.exe /DecodeHwHash=\"".odbc_result($exec, "HardwareID")."\">decode.log\r\n";
							$isFound="Pass";
						}
						odbc_close($conn);
					}
				}
			}
			odbc_close($conn);
		}else{
			// finding in new server
			$servername="DRIVER={SQL Server};SERVER=$ip;DATABASE=SmartClient";
			$conn=odbc_connect($servername,'thtfmes',"Mes&thtf",SQL_CUR_USE_ODBC);
			if ($conn){
				$sql="SELECT dbo.ProductKey.ProductKeyStateID, dbo.ProductKey.ProductKeyState
						FROM dbo.ProductKey WHERE dbo.ProductKey.MSFTProductKeyID = '$kid'";
				$exec=odbc_exec($conn, $sql);
				if(odbc_fetch_row($exec)){
					echo ":Product Key State:".odbc_result($exec, "ProductKeyState")."\r\n";
					echo "set KeyStateID=".odbc_result($exec, "ProductKeyStateID")."\r\n";

					if(odbc_result($exec, "ProductKeyStateID")>=3){
						$sql="SELECT dbo.ProductKeyInfo.ProductKeyID,dbo.ProductKeyInfo.HardwareID
								FROM dbo.ProductKey INNER JOIN
								dbo.ProductKeyInfo ON dbo.ProductKey.ProductKeyID = dbo.ProductKeyInfo.ProductKeyID
							WHERE (dbo.ProductKey.MSFTProductKeyID = '$kid')";

						$exec=odbc_exec($conn, $sql);
						if(odbc_fetch_row($exec)){
							echo ":Product Key ID:".odbc_result($exec, "ProductKeyID")."\r\n";
							echo ":MS Product Key ID:${kid}\r\n";
							echo "oa3tool.exe /DecodeHwHash=\"".odbc_result($exec, "HardwareID")."\">decode.log\r\n";
							$isFound="Bound";
						}else{
							echo ":[ERROR]: No KeyID was found\r\n";
						}
					}else{
						$isFound="Consumed";
					}
				}else{
					echo ":[ERROR]: No KeyID was found\r\n";
				}
			}else{
				echo ":[ERROR]: No KeyID was found";
			}
		}
		odbc_close($conn);
	}else{
		echo ":[ERROR]: information issue";
		echo ":ip=$ip";
		echo ":kid=$kid";
	}

	$fp=fopen("ffkilog.txt","a+");
	fputs($fp, "[".date("Y-m-d H:i:s")."]: $ip->$kid called and result is $isFound\r\n");
	fclose($fp);

?>